using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EnvDTE80;
using System.Diagnostics;
using Koda.VisualStudio;
using Koda.Search;

namespace Koda.UI
{
    public partial class OpenForm : Form
    {
        private IVsItemFinder finder;
        private bool isUpdating;

        [DefaultValue(true)]
        public bool KeywordTextBoxEnabled
        {
            get
            {
                return this.keywordTextBox.Enabled;
            }
            set
            {
                this.keywordTextBox.Enabled = value;
            }
        }

        public VsItem SelectedItem
        {
            get
            {
                return this.vsItemBindingSource.Current as VsItem;
            }
        }

        public OpenForm(IVsItemFinder finder)
        {
            if (finder == null)
                throw new ArgumentNullException("finder");

            this.isUpdating = true;
            this.finder = finder;
            this.AutoScaleMode = AutoScaleMode.None;
            this.Font = SystemFonts.MessageBoxFont;
            this.AutoScaleMode = AutoScaleMode.Font;

            this.InitializeComponent();

            this.Text = "Open";
            this.keywordTextBox.Text = finder.Keyword;
            this.vsItemBindingSource.DataSource = this.finder.Result;
            this.isUpdating = false;
        }

        private void OpenTypeForm_Load(object sender, EventArgs e)
        {
            this.keywordTextBox.Focus();
        }

        private void typeTextBox_TextChanged(object sender, EventArgs e)
        {
            if (!this.isUpdating)
            {
                this.Find();
            }
        }

        private void Find()
        {
            this.vsItemBindingSource.DataSource = null;
            this.numberOfItemsFoundLabel.Text = String.Empty;

            string text = this.keywordTextBox.Text.ToString();
            if (text != String.Empty)
            {
                this.finder.Search(text);
                this.vsItemBindingSource.DataSource = this.finder.Result;
            }
        }

        private void typeTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.vsItemBindingSource.Count > 0)
                {
                    this.vsItemBindingSource.Position++;
                    this.itemsDataGridView.Focus();
                }
            }
            else if (e.KeyCode == Keys.Enter)
            {
                this.OpenSelectedSymbol();
            }
        }

        private void symbolsDataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.OpenSelectedSymbol();
        }

        private void symbolsDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.OpenSelectedSymbol();
            }
        }

        private void openButton_Click(object sender, EventArgs e)
        {
            this.OpenSelectedSymbol();
        }
        
        private void OpenSelectedSymbol()
        {
            VsItem item = this.SelectedItem;
            if (item != null)
            {
                if (this.finder.OpenItem(item))
                {
                    this.DialogResult = DialogResult.OK;
                }
            }
        }

        private void vsItemBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (this.SelectedItem == null)
            {
                this.acceptButton.Enabled = false;
            }
            else
            {
                this.acceptButton.Enabled = true;
            }
        }

        private void vsItemBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            this.numberOfItemsFoundLabel.Text = String.Format("{0} items found", this.vsItemBindingSource.Count);
        }
    }
}